package com.xiaoc.bulldozer.common.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.xc.framework.cache.manager.CacheManager;
import com.xc.framework.cache.utils.CacheUtils;
import com.xiaoc.bulldozer.common.CacheKeys;
import com.xiaoc.bulldozer.common.Constant;
import com.xiaoc.bulldozer.common.dao.WeixinUserMapper;
import com.xiaoc.bulldozer.common.dto.WeixinUserDto;
import com.xiaoc.bulldozer.common.dto.WeixinUserQueryDto;
import com.xiaoc.bulldozer.common.model.WeixinUser;
import com.xiaoc.bulldozer.common.service.WeixinUserService;

/**
 * WeixinUser Service 实现类
 *
 * @author Danfo Yam
 * @date 2018-08-24 15:47:58
 */
@Service("weixinUserService")
public class WeixinUserServiceImpl implements WeixinUserService {

    @Resource
    private WeixinUserMapper weixinUserMapper;

    @Resource(name = "remoteCacheManager")
    private CacheManager remoteCacheManager;

    @Override
    public Integer getWeixinUserCountByCondition(WeixinUserQueryDto weixinUserQueryDto) {
        return weixinUserMapper.getWeixinUserCountByCondition(weixinUserQueryDto);
    }

    @Override
    public List<WeixinUser> getWeixinUserListByCondition(WeixinUserQueryDto weixinUserQueryDto) {
        return weixinUserMapper.getWeixinUserListByCondition(weixinUserQueryDto);
    }

    @Override
    public WeixinUser getWeixinUserById(Integer id) {
        return weixinUserMapper.getWeixinUserById(id);
    }

    @Override
    public List<WeixinUser> getAllWeixinUserList() {
        return weixinUserMapper.getAllWeixinUserList();
    }

    @Override
    public void save(WeixinUser weixinUser) {
        weixinUserMapper.save(weixinUser);
    }

    @Override
    public void update(WeixinUser weixinUser) {
        weixinUserMapper.update(weixinUser);
    }

    @Override
    public void deleteById(Integer id) {
        weixinUserMapper.deleteById(id);
    }

    @Override
    public WeixinUserDto getWeixinUserDtoBySessionId(String sessionId) {
        return StringUtils.isBlank(sessionId) ? null : (WeixinUserDto) remoteCacheManager.get(CacheUtils.generateCacheKey(CacheKeys.SESSION_WEIXIN_USER_KEY, sessionId));
    }

    @Override
    public void putWeixinUserDto(String sessionId, WeixinUserDto weixinUserDto) {
        weixinUserDto.setSessionTime(System.currentTimeMillis());
        remoteCacheManager.put(CacheUtils.generateCacheKey(CacheKeys.SESSION_WEIXIN_USER_KEY, sessionId), weixinUserDto, Constant.SESSION_TIMEOUT);
    }

    @Override
    public WeixinUser getWeixinUserByOpenId(String openId) {
        return weixinUserMapper.getWeixinUserByOpenId(openId);
    }

    @Override
    public List<WeixinUser> getWeixinUserListByPhoneNumber(String phoneNumber) {
        return weixinUserMapper.getWeixinUserListByPhoneNumber(phoneNumber);
    }

    @Override
    public List<WeixinUserDto> getWeixinUserList(WeixinUserQueryDto weixinUserQueryDto) {
        return weixinUserMapper.getWeixinUserList(weixinUserQueryDto);
    }

    @Override
    public Integer getWeixinUserCount(WeixinUserQueryDto weixinUserQueryDto) {
        return weixinUserMapper.getWeixinUserCount(weixinUserQueryDto);
    }
}
